package SortAll;

import java.util.Arrays;
import java.util.Random;

public class test {

    public static void func1(int []arr){//数据有序情况下非常快
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i;
        }

//        for (int i = 0; i < arr.length; i++) {//数据逆序情况下非常慢
//            arr[i] = arr.length-i;
//        }
    }

    public static void func2(int[] array) {//数据无序情况下比较慢
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] =  random.nextInt(10_0000);
        }
    }

    /**
     * 将数组当中 初始化为有序的数据
     * @param array
     */
    public static void initOrder(int[] array) {
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(100000);
            //array[i] = array.length-i;
            //array[i] = i;
        }
    }

    public static void testInsert(int[] array) {
        long start = System.currentTimeMillis();
        TestSort.insertSort(array);
        long end = System.currentTimeMillis();
        System.out.println("插入排序耗时："+(end-start));
    }

    public static void testShellSort(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long start = System.currentTimeMillis();
        TestSort.shellSort(array);
        long end = System.currentTimeMillis();
        System.out.println("希尔排序耗时："+(end-start));
    }


    public static void testSelectSort(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long start = System.currentTimeMillis();
        TestSort.selectSort(array);
        long end = System.currentTimeMillis();
        System.out.println("选择排序耗时："+(end-start));
    }

    public static void testHeapSort(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long start = System.currentTimeMillis();
        TestSort.heapSort(array);
        long end = System.currentTimeMillis();
        System.out.println("堆排序耗时："+(end-start));
    }

    public static void testbubbleSort(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long start = System.currentTimeMillis();
        TestSort.bubbleSort(array);
        long end = System.currentTimeMillis();
        System.out.println("冒泡排序耗时："+(end-start));
    }

    public static void testQuickSort(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long start = System.currentTimeMillis();
        TestSort.QuickSort(array);
        long end = System.currentTimeMillis();
        System.out.println("快速排序耗时："+(end-start));
    }

    public static void testMergeSort(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long start = System.currentTimeMillis();
        TestSort.MergeSort(array);
        long end = System.currentTimeMillis();
        System.out.println("归并排序耗时："+(end-start));
    }

    public static void main(String[] args) {
        int []arr = new int[1000000];
        initOrder(arr);
        //func2(arr);
//       testInsert(arr);
//       testShellSort(arr);
//       testSelectSort(arr);
     testHeapSort(arr);
//       testbubbleSort(arr);
       testQuickSort(arr);
        testMergeSort(arr);
    }
}
